package org.szgb.console.ht.electricity.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.ht.electricity.entity.HtElectricitySettlementBill;
import org.szgb.console.ht.electricity.entity.HtElectricitySettlementBillItem;
import org.szgb.console.ht.electricity.mapper.HtElectricitySettlementBillItemMapper;
import org.szgb.console.ht.electricity.mapper.HtElectricitySettlementBillMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class HtElectricitySettlementBillService {
    private final HtElectricitySettlementBillMapper htElectricitySettlementBillMapper;
    private final HtElectricitySettlementBillItemMapper htElectricitySettlementBillItemMapper;

    @Autowired
    public HtElectricitySettlementBillService(HtElectricitySettlementBillMapper htElectricitySettlementBillMapper, HtElectricitySettlementBillItemMapper htElectricitySettlementBillItemMapper) {
        this.htElectricitySettlementBillMapper = htElectricitySettlementBillMapper;
        this.htElectricitySettlementBillItemMapper = htElectricitySettlementBillItemMapper;
    }

    @Transactional(readOnly = true)
    public HtElectricitySettlementBill get(String id) {
        return htElectricitySettlementBillMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<HtElectricitySettlementBill> findList(HtElectricitySettlementBill htElectricitySettlementBill) {
        if (htElectricitySettlementBill != null && htElectricitySettlementBill.getPageDto() != null) {
            Page<HtElectricitySettlementBill> page = new Page<>(htElectricitySettlementBill.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            htElectricitySettlementBill.setPage(page);
        }
        return htElectricitySettlementBillMapper.findList(htElectricitySettlementBill);
    }

    @Transactional(readOnly = true)
    public Integer getCount(HtElectricitySettlementBill htElectricitySettlementBill) {
        return htElectricitySettlementBillMapper.getCount(htElectricitySettlementBill);
    }

    @Transactional()
    public void save(HtElectricitySettlementBill htElectricitySettlementBill) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (htElectricitySettlementBill.getId() == null || htElectricitySettlementBill.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            htElectricitySettlementBill.setCreatedBy(htElectricitySettlementBill.getUpdatedBy());
            // 添加UUID
            htElectricitySettlementBill.setId(UUIDUtil.getUUID());
            // 执行插入操作
            htElectricitySettlementBillMapper.insert(htElectricitySettlementBill);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            htElectricitySettlementBillMapper.update(htElectricitySettlementBill);
        }
    }

    @Transactional
    public void delete(String ids) {
        String[] strings = ids.split(",");
        htElectricitySettlementBillMapper.delete(strings);
        htElectricitySettlementBillItemMapper.deleteBySettlementBillId(strings);
    }

    @Transactional
    public void create(HtElectricitySettlementBill htElectricitySettlementBill, List<HtElectricitySettlementBillItem> itemList) {
        htElectricitySettlementBill.setCreatedBy(htElectricitySettlementBill.getUpdatedBy());
        // 添加UUID
        htElectricitySettlementBill.setId(UUIDUtil.getUUID());
        // 执行插入操作
        htElectricitySettlementBillMapper.insert(htElectricitySettlementBill);
        itemList.forEach(item -> {
            item.setId(UUIDUtil.getUUID());
            item.setHtElectricitySettlementBill(htElectricitySettlementBill);
            item.setCreatedBy(htElectricitySettlementBill.getUpdatedBy());
            item.setUpdatedBy(htElectricitySettlementBill.getUpdatedBy());
            htElectricitySettlementBillItemMapper.insert(item);
        });
    }
}
